(function (mui, $) {
var page = window.page = {};
page.tab = function (url) {
	$('.static.mui-bar-tab .mui-tab-item[data-url="'+url+'"]').trigger('tap');
};

localStorage.removeItem('dev');

mui.init({
	swipeBack           : false,//右滑关闭功能
	//statusBarBackground : '#E2032A',
	gestureConfig       : {
		doubletap  : true,
		popGesture :'none', //IOS下禁用右滑关闭
	},
	subpages:[
		{
			'url'     : 'html/member.html',
			'id'      : 'member.html',
			'styles'  : {'top':'0px', 'bottom':'50px'},
			'show'    : {autoShow:true},
			'waiting' : {autoShow:true},
		},
		{
			'url'     : 'html/cart.html',
			'id'      : 'cart.html',
			'styles'  : {'top':'0px', 'bottom':'50px'},
			'show'    : {autoShow:false},
			'waiting' : {autoShow:false},
		},
		{
			'url'     : 'html/discover.html',
			'id'      : 'discover.html',
			'styles'  : {'top':'0px', 'bottom':'50px'},
			'show'    : {autoShow:false},
			'waiting' : {autoShow:false},
		},
		{
			'url'     : 'html/category.html',
			'id'      : 'category.html',
			'styles'  : {'top':'0px', 'bottom':'50px'},
			'show'    : {autoShow:false},
			'waiting' : {autoShow:false},
		},
		{
			'url'     : 'html/home.html',
			'id'      : 'home.html',
			'styles'  : {'top':'0px', 'bottom':'50px'},
			'show'    : {autoShow:false},
			'waiting' : {autoShow:false},
		},
	]
});

mui.back = function() {
	//首次按键，提示‘再按一次退出应用’
	if (new Date().getTime() - app.esced < 2000) {
		plus.runtime.quit();
	} else {
		app.esced = new Date().getTime();
		mui.toast('再按一次退出应用');
	}
};

$('.static.mui-bar-tab .mui-tab-item').on('tap', function(e) {
	var dom  = this; e.stopPropagation();
	var prev = $('.static.mui-bar-tab .mui-tab-item.active').attr('data-url');
	var next = $(this).attr('data-url');
	
	if (prev == next) { return; }
	
	if ('member.html' == next && !app.store('member')) {
		app.open('html/member.login.html'); return;
	}
	
	$(dom).addClass("active").siblings().removeClass("active").removeClass("mui-active");
	plus.webview.show(next, 'pop-in', 100);
	
	try { plus.webview.getWebviewById(prev).hide(); } catch (e) {}
});

mui.plusReady(function(){
	plus.screen.lockOrientation('portrait');
	
	setTimeout(function () {
	try { plus.webview.getWebviewById('member.html').hide();   } catch (e) {}
	try { plus.webview.getWebviewById('cart.html').hide();     } catch (e) {}
	try { plus.webview.getWebviewById('discover.html').hide(); } catch (e) {}
	try { plus.webview.getWebviewById('category.html').hide(); } catch (e) {}
	}, 1000);
	
	setTimeout(function() { plus.nativeUI.closeWaiting(); }, 200);
	
	specified();
});
// 如果是通过URL协议打开应用的，需要进入指定页面
function specified () {
	var args = plus.runtime.arguments;
	//args = "lirlu-tx://cdlinglu.com?ally=1&id=101&uid=91&invitation=uykelvfl";
	if (!args) { return; }
	
	var p   = args.substr(args.indexOf('?') + 1);
	var res = {};
	var ary = p.split('&');
	for (var i = 0; i < ary.length; i++) {
		var e = ary[i].split('=');
		try { res[e[0]] = e[1]; } catch (e) {}
	}
	// 如果有商品ID
	if (/^\d+$/ig.test(res.id)) {
		app.open('html/product.detail.html', {'id':res.id, 'share':res});
	}
}

})(mui, $);
